package com.fingard.dsp.bank.directbank.ceb02;

import java.util.List;
import java.util.Map;

import com.fingard.dsp.bank.directbank.bankInterface.IRetailMsg;
import com.fingard.dsp.bank.reqrespobj.invoicemsg.ReqInvoiceMsgQryDetail;
import com.fingard.dsp.bank.reqrespobj.retailMsg.ReqRetailMsgQry;
import com.fingard.dsp.bank.reqrespobj.retailMsg.ReqRetailMsgQryDetail;
import com.fingard.dsp.bank.reqrespobj.retailMsg.RetAtsRetailMsg;
import com.fingard.xml.XElement;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

/**
 * 车辆零售信息
 * @author F
 *
 */
public class CEB02RetailMsg extends CEB02Base implements IRetailMsg{

    @Override
    public RetAtsRetailMsg  getRetailMsg(ReqRetailMsgQry p_orderMsg) throws Exception {
    	RetAtsRetailMsg retAts = new RetAtsRetailMsg(p_orderMsg.reqHeader);
        retAts.writeStart();
        String tmpRespCode = "";
        String tmpRespInfo = "";
        int pageNum = 50;
        int sendNum = 0;
        int i = 0;
        try {
            int total = Integer.valueOf(p_orderMsg.Z_BISHU);
            //每次推送40条
            int pageSize = total/pageNum;
            while(i < pageSize + 1) {
                CEB02Header bankHeader = new CEB02Header();
                //银行获取相关信息
                bankHeader.FTranCode = "Q060";
                XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
                //写入通用的头部节点
                writeCommonHeader_3(xmlWriter, bankHeader);
                //写入body
                xmlWriter.writeStartElement("Body");
                xmlWriter.writeElementString("CHANNEL_CODE", bankFront.org);//总笔数
                xmlWriter.writeElementString("Z_BISHU", p_orderMsg.Z_BISHU);//接入机构号 p_orderMsg.Z_BISHU
                while (sendNum < pageNum && p_orderMsg.readRecord()) {
                    sendNum ++;
                    ReqRetailMsgQryDetail tmpDetail = p_orderMsg.ReqDetail;
                    XElement tmpXElement = new XElement("Frame", "");
                    tmpXElement.addChild(new XElement("RETAIL_NO", tmpDetail.RetailNo));
                    tmpXElement.addChild(new XElement("BRAND_CODE", getBankConfig().getValueAsString("brandcode")));
                    tmpXElement.addChild(new XElement("DEALER_CODE",tmpDetail.DealerCode));
                    tmpXElement.addChild(new XElement("DEALER_NAME", tmpDetail.DealerName));
                    tmpXElement.addChild(new XElement("RETAIL_NUM", tmpDetail.RetailNum));
                    tmpXElement.addChild(new XElement("RETAIL_INVOICE_DATE", tmpDetail.RetailTime));
                    tmpXElement.addChild(new XElement("RETAIL_INVOICE_NO", tmpDetail.RetailInvoiceNo));
                    tmpXElement.addChild(new XElement("VEHICLE_CODE", tmpDetail.VIN));
                    tmpXElement.addChild(new XElement("CERTIFICATE_NO", tmpDetail.CertificationNo));
                    tmpXElement.addChild(new XElement("ENGINE_MODEL", tmpDetail.EngineNo));
                    tmpXElement.addChild(new XElement("CAR_MAKE_DATE", tmpDetail.CarMakeDate));
                    tmpXElement.addChild(new XElement("GOODS_UNIT", getBankConfig().getValueAsString("goodsunit")));
                    tmpXElement.addChild(new XElement("SELLING_PRICE", tmpDetail.SalePrice.equals("") ? "0" : tmpDetail.SalePrice));
                    tmpXElement.addChild(new XElement("RESERVE1", tmpDetail.Reserve1));
                    tmpXElement.addChild(new XElement("RESERVE2", tmpDetail.Reserve2));
                    tmpXElement.addChild(new XElement("RESERVE3", tmpDetail.Reserve3));
                    xmlWriter.writeXElement(tmpXElement);
                }
                xmlWriter.writeEndElement();
                xmlWriter.writeEndDocument();
                String[] tmpStrRet = sendToBank(xmlWriter.toXmlString(), bankHeader);//发送银行
                if (tmpStrRet[0].length() == 0) {
                    StringBuilder tmpSbMsg = new StringBuilder();
                    //解析银行返回
                    XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
                    if (tmpBkReader != null) {
                        String tmpRetCode = tmpBkReader.getFirstText("/Out/Head/ErrCode");
                        String tmpMsg = tmpBkReader.getFirstText("/Out/Body/ErrorInfo");
                        if (tmpRetCode.length() == 0) {
                            while (tmpBkReader.read("/Out/Body")) {
                                String tmpReturnCode = tmpBkReader.readedXElement.getSingleTextNotNull("RESULT_FLAG");
                                String tmpReturnMsg = tmpBkReader.readedXElement.getSingleTextNotNull("MSG");
                                if ("1".equals(tmpReturnCode)) {
                                    tmpRespCode = "0";
                                    tmpRespInfo = "处理成功";
                                } else {
                                    tmpRespCode = "-1";
                                    tmpRespInfo = tmpReturnCode + " : " + tmpReturnMsg;
                                }
                            }
                            i++;
                            //判断是否恰好满一次
                            if(sendNum*i == total) {
                            	break;
                            }else {
                            	sendNum = 0;
                            }
                        } else {
                            tmpRespCode = "-1";
                            tmpRespInfo = tmpMsg;
                            break;
                        }
                    } else {
                        tmpRespCode = "-1";
                        tmpRespInfo = tmpSbMsg.toString();
                        break;
                    }
                } else {
                    tmpRespCode = "-1";
                    tmpRespInfo = tmpStrRet[1];
                    break;
                }
            }
        } catch (Exception e) {
            tmpRespCode = "-1";
            tmpRespInfo = e.getMessage();
            e.printStackTrace();
        }
        retAts.writeRespCode(tmpRespCode);
        retAts.writeRespInfo(tmpRespInfo);
        retAts.writeEnd();
        return retAts;
    }


}
